<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input
        placeholder="请输入内容"
        v-model="form.name"
        class="search-main"
      />
      <el-button type="primary" @click="chaxun()">查询</el-button>
      <el-button type="primary" @click="add()">添加楼宇</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="tabledata">
        <el-table-column prop="index" label="序号" type="index" />
        <el-table-column label="楼宇名称" width="180" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积(m²)" prop="area" width="120" />
        <el-table-column
          label="物业费(元/m²)"
          prop="propertyFeePrice"
          width="120"
        />
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">闲置中</span>
            <span v-if="scope.row.status == 1">租赁中</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="shanchu(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-size="form.pageSize"
      />
    </div>
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <div class="form-container">
        <el-form :model="addForm" :rules="addFormRules" ref="addForm">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addbuild, builddelete, editbuild, getlouyu } from "@/api/user";

export default {
  name: "Building",
  data() {
    return {
      form: {
        page: 1,
        pageSize: 10,
      },
      title: "添加楼宇",
      tabledata: [],
      addForm: {},
      addFormRules: {
        name: [{ required: true, message: "请输入楼宇名称", trigger: "blur" }],
        floors: [
          { required: true, message: "请输入楼宇层数", trigger: "blur" },
        ],
        area: [{ required: true, message: "请输入在管面积", trigger: "blur" }],
        propertyFeePrice: [
          { required: true, message: "请输入物业费", trigger: "blur" },
        ],
      },
      total: 0,
      dialogVisible: false,
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    edit(row) {
      this.addForm = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      this.title = "编辑楼宇";
    },
    add() {
      this.addForm = {};
      this.dialogVisible = true;
      this.title = "新增楼宇";
    },
     shanchu(id) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await builddelete(id);
          if (res.code == 10000) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getdata();
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async submit() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.title == "新增楼宇") {
            const res = await addbuild(this.addForm);
            if (res.code == 10000) {
              this.dialogVisible = false;
              this.$message.success("成功");
              this.getdata();
            }
          } else {
            delete this.addForm.status;
            const res = await editbuild(this.addForm);
            if (res.code == 10000) {
              this.dialogVisible = false;
              this.$message.success("成功");
              this.getdata();
            }
          }
        }
      });
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    chaxun() {
      this.form.page = 1;
      this.form.pageSize = 10;
      this.getdata();
    },
    async getdata() {
      const res = await getlouyu(this.form);
      console.log(res, "res");
      this.tabledata = res.data.rows;
      this.total = res.data.total;
    },
    handleSizeChange(e) {
      this.form.pageSize = e;
      this.getdata();
    },
    handleCurrentChange(e) {
      this.form.page = e;
      this.getdata();
    },
  },
};
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-label {
    width: 100px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.building-container {
  padding: 10px;
  background: #fff;
}
</style>